<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>角色编辑</title>
    <script src="../../js/jquery.js"></script>
    <script src="../../layui/layui.js"></script>
    <link rel="stylesheet" href="../../layui/css/layui.css">
    <script src="../../js/jquery.cookei.js"></script>
    <script src="../../js/myAjax.js"></script>
    <script src="../../js/utlis/edit.js"></script>
</head>
<body>
<div style="margin-left: 80px; margin-top: 20px" class="layui-form">
    <div type="hidden" id="roleId"></div>
    <div class="layui-form-item">
        <label class="layui-form-label">角色名</label>
        <div class="layui-input-inline">
            <input type="text" name="title" required lay-verify="required" id="roleName" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">角色说明</label>
        <div class="layui-input-inline">
            <input type="text" name="title" required lay-verify="required" id="description" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">是否激活</label>
        <div class="layui-input-block">
            <input type="checkbox" value="1" name="switch" lay-skin="switch">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">创建时间</label>
        <div class="layui-input-inline">
            <input type="text" name="title" readonly required lay-verify="required" id="createTime" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" onclick="editAdd('/r/role/edit',getData())">立即提交</button>
            <button type="reset" onclick="resetEdit('/html/permission/roleEdit.html')"
                    class="layui-btn layui-btn-primary">
                点击重置
            </button>
        </div>
    </div>
</div>
<script>
    layui.use('form', function(){
        var form = layui.form;
        //监听提交
        form.on('submit(formDemo)', function(){
            // layer.msg(JSON.stringify(data.field));
            return false;
        });
    });
    function getData() {
        let data = {
            id: sessionStorage.getItem("roleId"),
            roleName: $("#roleName").val(),
            description: $("#description").val(),
            status: $('input[type=checkbox]:checked').val(),
            createTime: $("#createTime").val(),
        }
        if (data.status==null){
            data.status=0;
        }
        return data;
    }

    let id = sessionStorage.getItem("roleId")
    findById('/r/role/findById', id);

    function findById(url, id) {
        let result = myAjax(url, 'post', {id: id});
        $("#roleName").val(result.data.roleName);
        $("#description").val(result.data.description);
        if (result.data.status===1){
            $("input:checkbox[value='1']").attr('checked', 'true');
        }
        $("#createTime").val(result.data.createTime);
    }
</script>
</body>
</html>